libvirt

Libvirt is collection of software that provides a convenient way to manage virtual machines and other virtualization functionality, such as storage and network interface management.

  • These software pieces include:
    • a long term stable C API
    • a daemon (libvirtd)
    • a command line utility (virsh).

An primary goal of libvirt is to provide a single way to manage multiple different virtualization providers/hypervisors.
No need to learn the hypervisor specific tools.

For example: 'virsh list --all' listS the existing virtual machines for any supported hypervisor (KVM, Xen, VMWare ESX, etc.)

major functionality provided by libvirt?

Some of the major libvirt features are:

  • VM management:
    • Various domain lifecycle operations such as start, stop, pause, save, restore, and migrate.
    • Hotplug operations for many device types including disk and network interfaces, memory, and cpus.
  • Remote machine support
  • Storage management
  • Network interface management
  • Virtual NAT and Route based networking
In [ ]:
sudo apt-get install qemu-kvm
In [ ]:
sudo apt-get install libvirt-bin virtinstall virt-manager
In [ ]:
sudo gpasswd -a milad libvirtd
In [2]:
virsh list
 Id    Name                           State
----------------------------------------------------

In [1]:
virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

In [ ]:
virsh # go to interactive mode
In [3]:
virsh net-destroy --help
  NAME
    net-destroy - destroy (stop) a network

  SYNOPSIS
    net-destroy <network>

  DESCRIPTION
    Forcefully stop a given network.

  OPTIONS
    [--network] <string>  network name or uuid


stoping a network

In [5]:
virsh net-destroy default
Network default destroyed

In [8]:
virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------

In [7]:
virsh net-list --inactive # or --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              inactive   yes           yes

disabling autostart

In [10]:
virsh net-autostart --disable default
Network default unmarked as autostarted

In [11]:
virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              inactive   no            yes

KVM

KVM (Kernel-based Virtual Machine) is a full virtualization solution for Linux containing virtualization extensions (Intel VT or AMD-V).

  • It consists of :
    • a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure.
    • a processor specific module, kvm-intel.ko or kvm-amd.ko.

KVM does not support paravirtualization for CPU but may support paravirtualization for device drivers to improve I/O performance.
QEMU uses emulation; KVM uses processor extensions (HVM) for virtualization.

KVM kernel modules are part of the Linux kernel package Userspace tools are usually called "qemu-kvm" or "kvm" Linux guest drivers are part of the Linux kernel package

In [ ]:
sudo apt-get install cpu-checker
In [2]:
dpkg -L cpu-checker | grep bin/
/usr/sbin/kvm-ok
/usr/sbin/check-bios-nx

The program will first determine if /proc/cpuinfo contains the flags indicating that the CPU has the Virtualization Technology (VT) capability.
Next, it will check if the /dev/kvm device exists.

In [3]:
kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
In [6]:
egrep 'vmx|svm' -m1 -c /proc/cpuinfo  # match first one, just count; vmx: intel, svm: amd
1

"vmx" stands for Virtual Machine Extensions, which adds ten new instructions, These instructions permit entering and exiting a virtual execution mode where the guest OS perceives itself as running with full privilege (ring 0), but the host OS remains protected.

In [7]:
file /dev/kvm
/dev/kvm: character special (10/232)
In [9]:
lsmod | grep kvm
kvm_intel             172032  0
kvm                   544768  1 kvm_intel

QEMU

QEMU is a generic and open source machine emulator and virtualizer.

When used as a machine emulator, QEMU can run OSes and programs made for one machine (e.g. an ARM board) on a different machine (e.g. your own PC). By using dynamic translation, it achieves very good performance.

When used as a virtualizer, QEMU achieves near native performance by executing the guest code directly on the host CPU. QEMU supports virtualization when executing under the Xen hypervisor or using the KVM kernel module in Linux. When using KVM, QEMU can virtualize x86, server and embedded PowerPC, 64-bit POWER, S390, 32-bit and 64-bit ARM, and MIPS guests.

KVM (Kernel Virtual Machine) is a Linux kernel module that allows a user space program to utilize the hardware virtualization features of various processors. Today, it supports recent Intel and AMD processors (x86 and x86_64), PPC 440, PPC 970, S/390, ARM (Cortex A15, AArch64), and MIPS32 processors.

QEMU can make use of KVM when running a target architecture that is the same as the host architecture. For instance, when running qemu-system-x86 on an x86 compatible processor, you can take advantage of the KVM acceleration - giving you benefit for your host and your guest system.

The KVM project used to maintain a fork of QEMU called qemu-kvm. All feature differences have been merged into QEMU upstream and the development of the fork suspended.

To use KVM pass --enable-kvm to QEMU.

https://wiki.archlinux.org/index.php/Libvirt http://wiki.libvirt.org/page/FAQ#What_is_libvirt.3F http://www.linux-kvm.org/page/FAQ#General_KVM_information

Lecture notes

License

Creative Commons License

Linux Notes by Milad As (Ravexina) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


ravexina's gitlab

ravexina's github